using Microsoft.Extensions.DependencyInjection;
using Quartz;
using Quartz.Simpl;
using System;
using Zhao.Arena.Utility.QuartZJob;

namespace Zhao.Arena.Utility.QuartzExt
{
    /// <summary>
    /// Quartz定时任务扩展方法
    /// </summary>
    public static class QuartzExtensions
    {
        /// <summary>
        /// 添加所有Quartz定时任务
        /// </summary>
        /// <param name="builder">WebApplicationBuilder</param>
        public static WebApplicationBuilder AddAllQuartzJobs(this WebApplicationBuilder builder)
        {
            // 注册 Quartz 并配置标准任务
            builder.Services.AddQuartz(q =>
            {
                // 注册 Job 的推荐方式
                q.UseJobFactory<MicrosoftDependencyInjectionJobFactory>();

                //场地预约状态
                var fieldStatusKey = new JobKey("fJob");
                q.AddJob<FielReservationStatusJob>(opts => opts.WithIdentity(fieldStatusKey).StoreDurably());

                #region 场地预约状态修改 测试成功时间2025-6-16
                //// 添加初始触发器和每小时触发器
                //q.AddTrigger(opts => opts
                //    .ForJob(fieldStatusKey)
                //    .WithIdentity("fJob-initial-trigger")
                //    .WithSimpleSchedule(x => x.WithRepeatCount(0))); // 只执行一次
                #endregion

                q.AddTrigger(opts => opts
                    .ForJob(fieldStatusKey)
                    .WithIdentity("fJob-trigger")
                    .WithCronSchedule("0 0 0/1 * * ? "));//每个小时执行一次

                // 2. 活动状态检查任务
                var activityStatusKey = new JobKey("aJob");
                q.AddJob<ActivitiesStatusJbo>(opts => opts.WithIdentity(activityStatusKey).StoreDurably());


                #region 活动修改状态的定时任务测试成功时间2025-6-16
                //q.AddTrigger(opts => opts
                //  .ForJob(activityStatusKey)
                //  .WithIdentity("aJob-initial-trigger")
                //  .WithSimpleSchedule(x => x.WithRepeatCount(0))); // 只执行一次
                #endregion

                q.AddTrigger(opts => opts
                   .ForJob(activityStatusKey)
                   .WithIdentity("aJob-trigger")
                   .WithCronSchedule("0 0,30 * * * ?"));//半小时执行一次

                // 3. 结算任务
                var settlementKey = new JobKey("AutoSettlementJob");
                q.AddJob<AutoSettlementJob>(opts => opts.WithIdentity(settlementKey).StoreDurably());

                #region 结算活动和场地预约已完成订单的钱，球馆的定时任务测试成功时间2025-6-16
                //q.AddTrigger(opts => opts
                //  .ForJob(settlementKey)
                //  .WithIdentity("AutoSettlement-initial-trigger")
                //  .WithSimpleSchedule(x => x.WithRepeatCount(0))); // 只执行一次
                #endregion

                q.AddTrigger(opts => opts
                    .ForJob(settlementKey)
                    .WithIdentity("AutoSettlement-DailyTrigger")
                    .WithCronSchedule("0 0 2 * * ?")); // 每天凌晨2点执行
                    
                // 4. 历史订单状态更新任务
                var historicalOrderStatusKey = new JobKey("HistoricalOrderStatusJob");
                q.AddJob<HistoricalOrderStatusJob>(opts => opts.WithIdentity(historicalOrderStatusKey).StoreDurably());
                
                q.AddTrigger(opts => opts
                    .ForJob(historicalOrderStatusKey)
                    .WithIdentity("HistoricalOrderStatus-DailyTrigger")
                    .WithCronSchedule("0 0 1 * * ?")); // 每天凌晨1点执行
            });

            // 注册 Quartz 的后台服务
            builder.Services.AddQuartzHostedService(q => q.WaitForJobsToComplete = true);

            return builder;
        }

    }
}